package com.yikaiye.android.yikaiye.ui.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseFragment;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.FragmentGsInfoBinding;
import com.yikaiye.android.yikaiye.model.company.CompanyIntroModel;
import com.yikaiye.android.yikaiye.model.company.GsBaseInfoModel;
import com.yikaiye.android.yikaiye.model.company.GsInfoAllModel;
import com.yikaiye.android.yikaiye.ui.activity.mine.viewmodel.CompanyDetailsViewModel;
import com.yikaiye.android.yikaiye.ui.adapter.GsBaseInfoAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 工商信息
 */
public class CompanyGsInfoFragment extends BaseFragment<FragmentGsInfoBinding, CompanyDetailsViewModel> {

    private CompanyIntroModel companyIntroModel;
    private final List<GsInfoAllModel> data = new ArrayList<>();
    private GsBaseInfoAdapter adapter;

    public static CompanyGsInfoFragment newInstance(CompanyIntroModel companyIntroModel) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constant.MODEL_KEY, companyIntroModel);
        CompanyGsInfoFragment fragment = new CompanyGsInfoFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected CompanyDetailsViewModel bindModel() {
        return getViewModel(this, CompanyDetailsViewModel.class);
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            companyIntroModel = (CompanyIntroModel) getArguments().getSerializable(Constant.MODEL_KEY);
        }
        data.add(new GsInfoAllModel(getString(R.string.yyzzxx), R.mipmap.icon_base_info));
        data.add(new GsInfoAllModel(getString(R.string.gdxx), R.mipmap.icon_gd_info));
        data.add(new GsInfoAllModel(getString(R.string.zyryxx), R.mipmap.icon_zyry));
        data.add(new GsInfoAllModel(getString(R.string.gsbg), R.mipmap.icon_gsbg));
        data.add(new GsInfoAllModel(getString(R.string.qynb), R.mipmap.icon_qynb));
        adapter = new GsBaseInfoAdapter(mContext);
        binding.jyycRecyclerView.setAdapter(adapter);
        adapter.setEmptyViewEnable(true);
        adapter.setEmptyViewLayout(mContext, R.layout.layout_empty_list);
        viewModel.getGsBaseInfo(companyIntroModel.getEid());
        viewModel.getGsChangeRecord(companyIntroModel.getEid(), companyIntroModel.getCreditNo());
    }

    @Override
    protected void initClick() {
        // 基本信息返回结果
        viewModel.gsBaseInfoModelMutableLiveData.observe(this, gsBaseInfoModel -> {
            List<CompanyIntroModel> introData = getCompanyIntroModels(gsBaseInfoModel);
            data.get(0).setInfoList(introData);
            // 股东信息
            if (null != gsBaseInfoModel.getPartnersAlls() && !gsBaseInfoModel.getPartnersAlls().isEmpty()) {
                data.get(1).setPartnersAlls(gsBaseInfoModel.getPartnersAlls());
            }
            // 主要人员
            if (null != gsBaseInfoModel.getEmployeesAlls() && !gsBaseInfoModel.getEmployeesAlls().isEmpty()) {
                data.get(2).setEmployeesAlls(gsBaseInfoModel.getEmployeesAlls());
            }
            // 企业年报
            if (null != gsBaseInfoModel.getReportDetails() && !gsBaseInfoModel.getReportDetails().isEmpty()) {
                data.get(4).setReportDetails(gsBaseInfoModel.getReportDetails());
            }
            adapter.submitList(data);
        });

        // 基本信息返回结果错误
        viewModel.errorBaseInfoMutableLiveData.observe(this, s -> {
            List<CompanyIntroModel> introData = new ArrayList<>();
            companyIntroModel.setName(companyIntroModel.getCompanyName());
            introData.add(companyIntroModel);
            data.get(0).setInfoList(introData);
        });

        // 工商变更返回结果
        viewModel.gsChangeRecordDataModelMutableLiveData.observe(this, gsChangeRecordDataModel -> {
            if (null != gsChangeRecordDataModel.getRecords() && !gsChangeRecordDataModel.getRecords().isEmpty()) {
                data.get(3).setRecords(gsChangeRecordDataModel.getRecords());
            }
            adapter.submitList(data);
        });

        // 工商变更返回结果错误
        viewModel.errorGsChangeMutableLiveData.observe(this, s -> adapter.submitList(data));
    }

    private @NonNull List<CompanyIntroModel> getCompanyIntroModels(GsBaseInfoModel gsBaseInfoModel) {
        List<CompanyIntroModel> introData = new ArrayList<>();
        CompanyIntroModel companyIntroModel = new CompanyIntroModel(gsBaseInfoModel.getName(), gsBaseInfoModel.getCreditNo(), gsBaseInfoModel.getOperName(), gsBaseInfoModel.getRegistCapi(), gsBaseInfoModel.getStartDate(), gsBaseInfoModel.getNewStatusCode(), gsBaseInfoModel.getIndustryCodeList(), gsBaseInfoModel.getAddress(), gsBaseInfoModel.getReportAddress(), gsBaseInfoModel.getRegNo(), gsBaseInfoModel.getOrgNo(), gsBaseInfoModel.getBelongOrg(), gsBaseInfoModel.getCheckDate(), gsBaseInfoModel.getActualCapi(), gsBaseInfoModel.getEconKind(), gsBaseInfoModel.getTermStart(), gsBaseInfoModel.getTermEnd(), gsBaseInfoModel.getScope(), gsBaseInfoModel.getReportYear());
        introData.add(companyIntroModel);
        return introData;
    }
}
